home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Magazine / C_Tutorial / Part-6 / asl2 / idcmp.c < prev    next >
C/C++ Source or Header  |  1997-10-27  |  5KB  |  200 lines

  1. #include "idcmp.h"
  2. #include "drawwin.h"
  3. #include "gadgets.h"
  4. #include "loadsave.h"
  5. #include "menu.h"
  6. #include "toolwin.h"
  7.  
  8. #include<string.h>
  9.  
  10. #include<clib/exec_protos.h>
  11. #include<clib/gadtools_protos.h>
  12. #include<clib/graphics_protos.h>
  13. #include<clib/intuition_protos.h>
  14.  
  15. static void doGadgetUp(struct Window*, UWORD, struct Gadget*);
  16. static int  doMenuPick(struct Window*, UWORD);
  17.  
  18. /* Our message handling code */
  19. void handleIDCMP()
  20. {
  21.     char* text = "Hello World!";
  22.     int going = TRUE;
  23.     int drawing = FALSE;
  24.     struct Window* drawwin = getDrawWin();
  25.     ULONG drawsig, toolsig, gotsig;
  26.     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  27.     resetFgPen(drawwin);
  28.     while(going)
  29.     {
  30.         struct IntuiMessage* intuimsg;
  31.         /* Only include tool window signal mask if window is open */
  32.         toolsig = getToolSig();
  33.         /* Wait for messages to arrive */
  34.         gotsig = Wait(drawsig | toolsig);
  35.         /* Messages have arrived: loop through all of them */
  36.         /* Check messages from the drawing window first */
  37.         if(gotsig & drawsig)
  38.         {
  39.             while(intuimsg = GT_GetIMsg(drawwin->UserPort))
  40.             {
  41.                 /* Copy the important bits of the message */
  42.                 ULONG class = intuimsg->Class;
  43.                 UWORD code = intuimsg->Code;
  44.                 WORD mousex = intuimsg->MouseX;
  45.                 WORD mousey = intuimsg->MouseY;
  46.                 /* Reply when finished copying bits from message */
  47.                 GT_ReplyIMsg(intuimsg);
  48.                 /* Act on this message... */
  49.                 switch(class)
  50.                 {
  51.                 case IDCMP_MOUSEBUTTONS:
  52.                     switch(code)
  53.                     {
  54.                     case SELECTDOWN:
  55.                         drawing = TRUE;
  56.                         break;
  57.                     case SELECTUP:
  58.                         drawing = FALSE;
  59.                         break;
  60.                     }
  61.                     /* break; omitted so we draw on click, too */
  62.                 case IDCMP_MOUSEMOVE:
  63.                     if(drawing)
  64.                     {
  65.                         Move(drawwin->RPort, mousex, mousey);
  66.                         Text(drawwin->RPort, text, strlen(text));
  67.                     }
  68.                     break;
  69.                 case IDCMP_MENUPICK:
  70.                     going = doMenuPick(drawwin, code);
  71.                     break;
  72.                 }
  73.             }
  74.         }
  75.         /* Now check messages from the tool window */
  76.         if(going && (gotsig & toolsig))
  77.         {
  78.             struct Window* toolwin = getToolWin();
  79.             while(toolwin && (intuimsg = GT_GetIMsg(toolwin->UserPort)))
  80.             {
  81.                 /* Copy the important bits of the message */
  82.                 ULONG class = intuimsg->Class;
  83.                 UWORD code = intuimsg->Code;
  84.                 APTR iaddr = intuimsg->IAddress;
  85.                 /* Reply when finished copying bits from message */
  86.                 GT_ReplyIMsg(intuimsg);
  87.                 /* Act on this message... */
  88.                 switch(class)
  89.                 {
  90.                 case IDCMP_CLOSEWINDOW:
  91.                     closeToolWin();
  92.                     /* Update our local toolwin, so we stop loop */
  93.                     toolwin = NULL;
  94.                     uncheckToolBar(drawwin);
  95.                     break;
  96.                 case IDCMP_REFRESHWINDOW:
  97.                     /* You *MUST* remember to ask for and handle these refresh messages */
  98.                     GT_BeginRefresh(toolwin);
  99.                     GT_EndRefresh(toolwin, TRUE);
  100.                     break;
  101.                 case IDCMP_GADGETUP:
  102.                     doGadgetUp(drawwin, code, (struct Gadget*)iaddr);
  103.                     break;
  104.                 case IDCMP_MENUPICK:
  105.                     going = doMenuPick(drawwin, code);
  106.                     /* Update our local toolwin, so we stop loop */
  107.                     toolwin = getToolWin();
  108.                     break;
  109.                 }
  110.             }
  111.         }
  112.     }
  113. }
  114.  
  115. /* Process IDCMP_GADGETUP event */
  116. static void doGadgetUp(struct Window* drawwin, UWORD code, struct Gadget* gad)
  117. {
  118.     switch(gad->GadgetID)
  119.     {
  120.     case MYBUT_ID:
  121.         /* Our button was clicked!  Set foreground to next pen colour */
  122.         nextFgPen(drawwin);
  123.         break;
  124.     case MYPAL_ID:
  125.         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  126.         setFgPen(drawwin, code);
  127.         break;
  128.     }
  129. }
  130.  
  131. /* Process IDCMP_MENUPICK event */
  132. static int doMenuPick(struct Window* drawwin, UWORD code)
  133. {
  134.     struct Menu* menu = drawwin->MenuStrip;
  135.     UWORD menuCode, menuNumber, itemNumber;
  136.     /* Loop over all the menu selections in the menu code */
  137.     struct MenuItem* item;
  138.     for(menuCode = code;
  139.             menuCode != MENUNULL;
  140.             menuCode = item->NextSelect)
  141.     {
  142.         item = ItemAddress(menu, menuCode);
  143.         /* Extract the menu number and menu item number from the menu code */
  144.         menuNumber = MENUNUM(menuCode);
  145.         itemNumber = ITEMNUM(menuCode);
  146.         /* Now decide what to do based on what menu item was selected */
  147.         switch(menuNumber)
  148.         {
  149.         case 0:  /* Project menu */
  150.             /* Only one item: Quit */
  151.             switch(itemNumber)
  152.             {
  153.             case 0:  /* Load */
  154.                 load(drawwin);
  155.                 break;
  156.             case 1:  /* Save */
  157.                 save(drawwin);
  158.                 break;
  159.             case 3:  /* Quit (item 2 is the bar!) */
  160.                 return FALSE;
  161.             }
  162.             break;
  163.         case 1:  /* Pen menu */
  164.             switch(itemNumber)
  165.             {
  166.             case 0:  /* Next */
  167.                 nextFgPen(drawwin);
  168.                 break;
  169.             case 1:  /* Prev */
  170.                 prevFgPen(drawwin);
  171.                 break;
  172.             case 3:  /* Reset (item 2 is the bar!) */
  173.                 resetFgPen(drawwin);
  174.                 break;
  175.             }
  176.             break;
  177.         case 2:  /* Tools menu */
  178.             switch(itemNumber)
  179.             {
  180.             case 0:  /* Screen Bar */
  181.                 ShowTitle(drawwin->WScreen, item->Flags & CHECKED);
  182.                 break;
  183.             case 1:  /* Tool Bar */
  184.                 /* Do the open or close */
  185.                 if(item->Flags & CHECKED)
  186.                 {
  187.                     /* If the open fails, stop immediately */
  188.                     if(!openToolWin())
  189.                         return FALSE;
  190.                 }
  191.                 else
  192.                     closeToolWin();
  193.                 break;
  194.             }
  195.         }
  196.     }
  197.     /* Keep going */
  198.     return TRUE;
  199. }
  200.